﻿namespace rmfast
{
    internal class Program
    {
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine("use: rmfast pathtodel ");
            }
            rmfast(args[0]);
        }


        static void rmfast(string path)
        {
            if (System.IO.Directory.Exists(path))
            {
                var files = System.IO.Directory.GetFiles(path);
                var dirs = System.IO.Directory.GetDirectories(path);
                foreach (var f in files)
                {
                    try
                    {
                        System.IO.File.Delete(f);
                    }
                    catch (Exception err)
                    {
                        Console.Error.WriteLine("delete file error:" + f);
                    }

                }
                int finishcount = 0;
                int errorcount = 0;
                Parallel.ForEach(dirs, (dirpath) =>
                {
                    var b = DeletePath(dirpath);
                    if (b)
                        finishcount++;
                    else
                        errorcount++;

                    Console.WriteLine("==========>Delete Root dir count=" + dirs.Length + " Finish=" + finishcount + " Error=" + errorcount);
                });
                //foreach (var p in dirs)
                //{
                //    var b = DeletePath(p);
                //    if (b)
                //        finishcount++;
                //    else
                //        errorcount++;

                //    Console.WriteLine("==========>Delete Root dir count=" + dirs.Length + " Finish=" + finishcount + " Error=" + errorcount);
                //}
            }
            else
            {
                Console.WriteLine("Path do not exist.");
                return;
            }

        }
        static bool DeletePath(string path)
        {
            bool succ = true;
            var files = System.IO.Directory.EnumerateFiles(path);
            var dirs = System.IO.Directory.EnumerateDirectories(path);
            foreach (var f in files)
            {
                try
                {
                    System.IO.File.SetAttributes(f, FileAttributes.Normal);
                    System.IO.File.Delete(f);
                }
                catch (Exception err)
                {
                    Console.Error.WriteLine("delete file error:" + f);
                    succ = false;
                }
            }
            foreach (var p in dirs)
            {
                var b = DeletePath(p);
                if (!b) succ = false;
            }
            if (succ)
            {
                try
                {
                    Console.Error.WriteLine("delete path:" + path);
                    System.IO.Directory.Delete(path);
                }
                catch (Exception err)
                {
                    Console.Error.WriteLine("delete path error:" + path);
                    succ = false;
                }
            }
            
            return succ;
        }
    }
}
